package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import com.google.code.geocoder.Geocoder;
import com.google.code.geocoder.GeocoderRequestBuilder;
import com.google.code.geocoder.model.GeocodeResponse;
import com.google.code.geocoder.model.GeocoderRequest;

import communication.Order;

import structures.Location;
import system.AgentManager;
import system.JadeManager;
import system.SendOrderBehaviour;

public class MyFrame extends JFrame {

	private JPanel contentPane;
	private JadeManager jademanager;

	JTextArea taText;
	JEditorPane epAdress;

	/**
	 * Create the frame.
	 * 
	 * @param jadeManager
	 */
	public MyFrame(JadeManager jadeManager) {
		setTitle("Client Program");

		this.jademanager = jadeManager;

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 819, 495);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		setContentPane(contentPane);

		taText = new JTextArea();
		taText.setBounds(37, 51, 400, 308);
		contentPane.add(taText);

		JButton btnSend = new JButton("SEND");
		btnSend.setBounds(333, 403, 89, 23);
		contentPane.add(btnSend);

		epAdress = new JEditorPane();
		epAdress.setBounds(483, 124, 212, 118);
		contentPane.add(epAdress);

		JLabel lblProblem = new JLabel("Problem");
		lblProblem.setBounds(37, 26, 137, 14);
		contentPane.add(lblProblem);

		JLabel lblAdress = new JLabel("Address");
		lblAdress.setBounds(483, 99, 116, 14);
		contentPane.add(lblAdress);

		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendOrder();
			}
		});

		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				JadeManager.deletaAllAgents();
				System.out.println("EXITING");
			}
		});
	}

	protected void sendOrder() {
		Order order = new Order(AgentManager.agent.getAID(),
				AgentManager.agent.getLocalName() +" - "+ new Date().toString(),
				taText.getText());

		final Geocoder geocoder = new Geocoder();
		String address = epAdress.getText();
		GeocoderRequest geocoderRequest = new GeocoderRequestBuilder()
				.setAddress(address).setLanguage("pl").getGeocoderRequest();
		try {
			GeocodeResponse geocoderResponse = geocoder
					.geocode(geocoderRequest);
			System.out.println(geocoderResponse);
			System.out.println(geocoderResponse.getResults().get(0)
					.getGeometry().getLocation());

			double latitude = geocoderResponse.getResults().get(0)
					.getGeometry().getLocation().getLat().doubleValue();
			double longitude = geocoderResponse.getResults().get(0)
					.getGeometry().getLocation().getLng().doubleValue();
			order.setLocation(new Location(latitude, longitude));
			if (order != null)
				AgentManager.getAgent().addBehaviour(
						new SendOrderBehaviour(order));

			System.out.println(geocoderResponse.getResults().get(0)
					.getGeometry().getLocation());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
